package org.com.lcuiot.security;

import org.com.lcuiot.service.AccountService;
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

import java.util.Optional;

/**
 * 数据库数据生成认证信息对比
 *
 * @author gaoge
 * @since 2022/10/27 15:29
 */
@Component
public class CustomUserDetailsService implements ReactiveUserDetailsService {


    private final AccountService accountService;

    public CustomUserDetailsService(AccountService accountService) {
        this.accountService = accountService;
    }


    @Override
    public Mono<UserDetails> findByUsername(String username) {
        Optional<JwtUser> jwtUser = accountService.selectByUsername(username);
        return Mono.just(jwtUser
                .orElseThrow(() -> new UsernameNotFoundException("Username: " + username + " not found"))
        );
    }
}